#include "WPH_server.h"
#include "account.h"
#include "collectInfo.h"
#include "state.h"
#include "sys_log.h"

//采集信息
void gatherInfo(int sock,char *buf)
{
    char type[30] = {0};
    strcpy(type,funType(buf,type));//取出type
    struct pump info =  infoGather(buf);//解包
    char sql[1024] = {0};

    LOG_D("[%d] type : %s\n",__LINE__ ,type);

    MYSQL *mysql = NULL;
  	MYSQL *rs = NULL;
    mysql = mysql_init(NULL);
    int ret = 0;
    int value = 0;

	rs = mysql_real_connect(mysql,"localhost","","","water",0,NULL,0);
	if(rs == NULL)
	{
		LOG_E("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
 	}

    LOG_D("[%d] %s, %d, %d, %d, %f, %s, %d\n",__LINE__,info.time,info.flow,info.temperature,info.liquidlevel,info.stress,info.valve,info.id);

    if(strcmp(type,"wph1") == 0)//采集1号
    {
        sprintf(sql,"insert into wph1 values ('%s',%d,%d,%d,%f,'%s',%d)",info.time,info.flow,info.temperature,info.liquidlevel,info.stress,info.valve,info.id);
        ret = mysql_query(mysql,sql);
        if(ret != 0)
        {
            LOG_E("%s\n",mysql_error(mysql));
            mysql_close(mysql);
            exit(-1);
        }
        printf("插入成功！\n");
    }
    else if(strcmp(type,"wph2") == 0)//采集2号
    {  
        sprintf(sql,"insert into wph2 values ('%s',%d,%d,%d,%f,'%s',%d)",info.time,info.flow,info.temperature,info.liquidlevel,info.stress,info.valve,info.id);
        ret = mysql_query(mysql,sql);
        if(ret != 0)
        {
            printf("%s\n",mysql_error(mysql));
            mysql_close(mysql);
            exit(-1);
        }
        printf("插入成功！\n");
    }
    else if(strcmp(type,"wph3") == 0)//采集3号
    {
        sprintf(sql,"insert into wph3 values ('%s',%d,%d,%d,%f,'%s',%d)",info.time,info.flow,info.temperature,info.liquidlevel,info.stress,info.valve,info.id);
        ret = mysql_query(mysql,sql);
        if(ret != 0)
        {
            printf("%s\n",mysql_error(mysql));
            mysql_close(mysql);
            exit(-1);
        }
        printf("插入成功！\n");
    }
    mysql_close(mysql);
}

//采集信息解包
struct pump infoGather(char *buf)
{
    struct pump info ;
    memset(&info,0,sizeof(info));
    char type[20] = {0};

    //定义cJSON指针
    cJSON *json = NULL;
    //将字符串转为json格式对象
    json = cJSON_Parse(buf);

    cJSON *node = NULL;
    //由键获取值
    node = cJSON_GetObjectItem(json,"type");
    if(node == NULL)
    {
        LOG_E("can't find type\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcmp(type,node->valuestring);
    }

    node = cJSON_GetObjectItem(json,"id");
    if(node == NULL)
    {
        LOG_E("can't find type\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.id = node->valueint;
    }
    
    node = cJSON_GetObjectItem(json,"time");
    if(node == NULL)
    {
        LOG_E("can't find time\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(info.time,node->valuestring);
    }

    node = cJSON_GetObjectItem(json,"flow");
    if(node == NULL)
    {
        LOG_E("can't find name\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.flow = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"temperature");
    if(node == NULL)
    {
        LOG_E("can't find gender\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.temperature = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"liquidlevel");
    if(node == NULL)
    {
        LOG_E("can't find age\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.liquidlevel = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"stress");
    if(node == NULL)
    {
        LOG_E("can't find account\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.stress = node->valuedouble;
    }

    node = cJSON_GetObjectItem(json,"valve");
    if(node == NULL)
    {
        LOG_E("can't find cipher\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(info.valve,node->valuestring);
    }
    cJSON_Delete(json);	
    return info;
}